
package com.fatcat.easybuy.controller;

import com.fatcat.easybuy.annotaion.CheckPermission;
import com.fatcat.easybuy.exception.BusinessException;
import com.fatcat.easybuy.pojo.EasyBuyNews;
import com.fatcat.easybuy.service.EasybuyNewsService;
import com.fatcat.easybuy.util.ResponseResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Api(tags = "新闻模块", description = "新闻模块接口")
@RestController
@RequestMapping(value = "/news")
public class EasyBuyNewsController {
    @Autowired
    private EasybuyNewsService easybuyNewsService;


    @ApiOperation(value = "新增新闻", notes = "新增新闻")
    @ApiImplicitParam(name = "easybuyNews", value = "新闻对象", required = true, dataType = "EasybuyNews", paramType = "form")
    @CheckPermission("0")
    @PostMapping(value = "/addNews")
    public ResponseResult addNews(
            @Validated// 校验参数
            @RequestBody EasyBuyNews easybuyNews) throws BusinessException {
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        easybuyNews.setCreateTime(sdf.format(new Date()));
        // 新增新闻
        if (!easybuyNewsService.addNews(easybuyNews)) {
            return ResponseResult.error("新增新闻失败");
        }
        // 返回结果
        return ResponseResult.success("新增新闻成功");
    }
    @ApiOperation(value = "删除新闻", notes = "删除新闻")
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, dataType = "Integer", paramType = "path")
    @CheckPermission("0")
    @DeleteMapping(value = "/deleteNewsById/{id}")
    public ResponseResult deleteNews(
            @PathVariable Integer id) throws BusinessException {
        // 校验参数
        if (id == null||id<=0) {
            return ResponseResult.error("id不能为空");
        }
        // 删除新闻
        if (!easybuyNewsService.deleteNewsById(id)) {
            return ResponseResult.error("删除新闻失败");
        }
        // 返回结果
        return ResponseResult.success("删除新闻成功");
    }
    @ApiOperation(value = "修改新闻", notes = "修改新闻")
    @ApiImplicitParam(name = "easybuyNews", value = "新闻对象", required = true, dataType = "EasybuyNews", paramType = "form")
    @CheckPermission("0")
    @PostMapping(value = "/updateNews")
    public ResponseResult updateNews(
            @Validated// 校验参数
            @RequestBody   EasyBuyNews easybuyNews) throws BusinessException {
        // 校验参数
        if (easybuyNews.getId() == null||easybuyNews.getId()<=0) {
            return ResponseResult.error("id不能为空");
        }
        // 修改新闻
        if (!easybuyNewsService.updateNews(easybuyNews)) {
            return ResponseResult.error("修改新闻失败");
        }
        // 返回结果
        return ResponseResult.success("修改新闻成功");
    }

    @ApiOperation(value = "根据id获取新闻", notes = "根据id获取新闻")
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping(value = "/getNewsById/{id}")
    public ResponseResult getNewsById(@PathVariable Integer id) {
        if (id == null||id<=0) {
            return ResponseResult.error("id不能为空");
        }
        EasyBuyNews news = easybuyNewsService.getNewsById(id);
        if (news == null) {
            return ResponseResult.error("根据id获取新闻失败");
        }
        return ResponseResult.success("根据id获取新闻成功").add("news", news);
    }
    @ApiOperation(value = "获取所有新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", required = false, dataType = "String", paramType = "yyyy-MM-dd"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "Integer", paramType = "query")
    })
    @GetMapping(value = "/getAllNews")
    public ResponseResult getAllNews(@RequestParam Map<String, Object> params) {

        PageInfo<EasyBuyNews> pageInfo = easybuyNewsService.getAllNews(params);

        return ResponseResult.success("获取新闻分页信息成功").add("pageInfo", pageInfo);
    }




}
